/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.form;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.topdt.jbpm.Constants;

/**
 * 建立日期 : 2013-5-20 上午11:47:14<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class FormUI {

	private UIType uiType;// ui类型

	private String uiURL;// ui的URL

	private byte[] uiByte;//

	private String uiText;

	private Map fieldControl;
	private Map funcControl;
	private Document doc;
	private Map<String, Object> context;

	public enum UIType {
		textXml, textJson, textHtml, url, iByte
	}

	public FormUI(Map fieldControl, Map funcControl, Document doc) {
		this.fieldControl = fieldControl;
		this.funcControl = funcControl;
		this.doc = doc;
	}

	public FormUI(Document doc, Map<String, Object> context) {
		this.doc = doc;
		this.fieldControl = (Map<String, String>) context.get("fieldControl");
		this.funcControl = (Map<String, String>) context.get("funcControl");
		this.context = context;
	}

	public void response(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setAttribute("app", context.get("appObj"));
		request.setAttribute("work", context.get("workObj"));
		request.setAttribute("user", context.get("userObj"));
		request.setAttribute("todo", context.get("todoObj"));
		request.setAttribute("readOnly", context.get("readOnly"));
		request.setAttribute("menuCtl", context.get("menuCtl"));
		request.setAttribute("menuCtlJson", context.get("menuCtlJson"));

		request.setAttribute("fieldCtl", context.get("fieldCtl"));
		request.setAttribute("fieldCtlJson", context.get("fieldCtlJson"));

		request.setAttribute("opinionCtl", context.get("opinionCtl"));
		request.setAttribute("opinionCtlJson", context.get("opinionCtlJson"));

		request.setAttribute("document", doc);

		switch (this.uiType) {

		case textXml:
			response.setContentType(Constants.CONTENT_TYPE_XML);
			response.getWriter().write(this.uiText);
			break;
		case textJson:
			response.setContentType(Constants.CONTENT_TYPE_JSON);
			response.getWriter().write(this.uiText);
			break;
		case textHtml:
			response.setContentType(Constants.CONTENT_TYPE_HTML);
			response.getWriter().write(this.uiText);
			break;
		case url:
			request.setAttribute("fieldControl", fieldControl);
			request.setAttribute("funcControl", funcControl);
			request.setAttribute("document", doc);
			RequestDispatcher dispatcher = request.getRequestDispatcher(this.uiURL);
			dispatcher.forward(request, response);
			break;
		case iByte:
			response.setContentType(Constants.CONTENT_TYPE_HTML);
			response.getOutputStream().write(this.uiByte);
			break;
		default:
			response.setContentType(Constants.CONTENT_TYPE_TEXT);
			response.getWriter().print("业务表单不正确！");
		}

	}

	public UIType getUiType() {
		return uiType;
	}

	public void setUiType(UIType uiType) {
		this.uiType = uiType;
	}

	public String getUiURL() {
		return uiURL;
	}

	public void setUiURL(String uiURL) {
		this.uiURL = uiURL;
	}

	public byte[] getUiByte() {
		return uiByte;
	}

	public void setUiByte(byte[] uiByte) {
		this.uiByte = uiByte;
	}

	public String getUiText() {
		return uiText;
	}

	public void setUiText(String uiText) {
		this.uiText = uiText;
	}

	public Map getFieldControl() {
		return fieldControl;
	}

	public void setFieldControl(Map fieldControl) {
		this.fieldControl = fieldControl;
	}

	public Map getFuncControl() {
		return funcControl;
	}

	public void setFuncControl(Map funcControl) {
		this.funcControl = funcControl;
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

	public Map<String, Object> getContext() {
		return context;
	}

	public void setContext(Map<String, Object> context) {
		this.context = context;
	}
}
